﻿#region Imports
using System.Drawing;
using RS.Controls.Timeline.Services;
#endregion


namespace RS.Controls.Timeline
{
    /// <summary>
    /// Basic time slice class. Use this as the base clase for your own time
    /// slice classes.
    /// </summary>
    public class TimeSlice : ITimeSlice
    {
        #region Fields
        private int _x, _y;
        private int _w, _h;
        private bool _isActive;
        private bool _isTicked;
        private Color _c = Color.DarkGray;
        private Color _highlightColor = Color.Red;
        #endregion //Fields

        #region Constructor
        /// <summary>
        /// Initializes a new instance of the <see cref="TimeSlice"/> class.
        /// </summary>
        /// <param name="x">The x position of the time slice</param>
        /// <param name="y">The y position of the time slice</param>
        /// <param name="w">The width of the time slice</param>
        /// <param name="h">The height of the time slice</param>
        public TimeSlice(int x, int y, int w, int h)
        {
            _x = x;
            _y = y;
            _h = h;
            _w = w;
        }
        #endregion //Constructor

        #region Properties
        /// <summary>
        /// The x location.
        /// </summary>
        /// <value>The X.</value>
        public int X
        {
            get { return _x; }
            set { _x = value; }
        }

        /// <summary>
        /// Gets or sets the Y.
        /// </summary>
        /// <value>The Y.</value>
        public int Y
        {
            get { return _y; }
            set { _y = value; }
        }

        /// <summary>
        /// Gets or sets the W.
        /// </summary>
        /// <value>The W.</value>
        public int W
        {
            get { return _w; }
            set { _w = value; }
        }

        /// <summary>
        /// Gets or sets the H.
        /// </summary>
        /// <value>The H.</value>
        public int H
        {
            get { return _h; }
            set { _h = value; }
        }

        /// <summary>
        /// Gets or sets the color.
        /// </summary>
        /// <value>The color.</value>
        public Color Color
        {
            get { return _c; }
            set { _c = value; }
        }

        /// <summary>
        /// Gets or sets the color of the highlight.
        /// </summary>
        /// <value>The color of the highlight.</value>
        public Color HighlightColor
        {
            get { return _highlightColor; }
            set { _highlightColor = value; }
        }

        /// <summary>
        /// Gets or sets a value indicating whether this instance is active.
        /// </summary>
        /// <value><c>true</c> if this instance is active; otherwise, <c>false</c>.</value>
        public bool IsActive
        {
            get { return _isActive; }
            set { _isActive = value; }
        }

        /// <summary>
        /// Gets or sets a value indicating whether this instance is ticked.
        /// </summary>
        /// <value><c>true</c> if this instance is ticked; otherwise, <c>false</c>.</value>
        public bool IsTicked
        {
            get { return _isTicked; }
            set { _isTicked = value; }
        }

        #endregion //Properties

        #region Methods
        /// <summary>
        /// This method is executed when the time slice is activated.
        /// </summary>
        public void Action()
        {

        }

        /// <summary>
        /// Draws the specified g.
        /// </summary>
        /// <param name="g">The g.</param>
        public void Draw(Graphics g)
        {
            Rectangle rect = new Rectangle(_x, _y, _w, _h);

            //Draw the background
            SolidBrush sBrush = new SolidBrush(_c);
            g.FillRectangle(sBrush, rect);

            if (_isTicked) // Draw the tick
            {
                sBrush = new SolidBrush(Color.Brown);
                g.FillRectangle(sBrush, _x+_w, 5, 2, _h);
            }

            // Draw the highlight boarder
            if (_isActive)
            {
                sBrush = new SolidBrush(_highlightColor);
                g.DrawRectangle(new Pen(sBrush), _x, _y, _w, _h-1);
            }
        }

        /// <summary>
        /// Determines whether the specified x position is  is collision.
        /// </summary>
        /// <param name="x">The X.</param>
        /// <returns>
        /// 	<c>true</c> if the specified X is collision; otherwise, <c>false</c>.
        /// </returns>
        public bool IsCollision(int x)
        {
            return (x <= (_x + _w) && x > _x);
        }

        /// <summary>
        /// Determines whether the specified x1 is collision.
        /// </summary>
        /// <param name="x1">The x1.</param>
        /// <param name="x2">The x2.</param>
        /// <returns>
        /// 	<c>true</c> if the specified x1 is collision; otherwise, <c>false</c>.
        /// </returns>
        public bool IsCollision(int x1, int x2)
        {
            if (x2 > x1)
            {
                return (x1 <= (_x + _w) && x2 >= _x);
            }
            else
            {
                return (x2 <= (_x + _w) && x1 >= _x);
            }
        }
        #endregion //Methods
    }
}
